<?php
/**
 * Created by PhpStorm.
 * 商品sku模型
 * User: caicm
 * Date: 2017/6/19
 * Time: 17:21
 */

namespace WMApi\Model;


use Think\Model;

class WmGoodsSkuModel extends Model {

    /**
     * 添加到sku表
     * Created By caicm
     * @param array $sku_data
     * @param $goods_id
     * @return bool|string
     */
    public function add_sku($sku_data=array(), $goods_id){

        $data = array();
        foreach ($sku_data as $key=>$value){
            $data[] = array(
                'goods_id' => $goods_id,
                'sku_code' => get_goods_sn('s'),
                'stock_num' => 0,
                'add_time' => gmtime(),
                'is_del' => 0,
                'goods_money' => $value['goods_money'],
                'main_attr_id' => $value['main_attr_id'],
                'main_attr_name' => $value['main_attr_name'],
                'main_attr_value_id' => $value['main_attr_value_id'],
                'main_attr_value_name' => $value['main_attr_value_name'],
                'sec_attr_id' => $value['sec_attr_id'],
                'sec_attr_name' => $value['sec_attr_name'],
                'sec_attr_value_id' => $value['sec_attr_value_id'],
                'sec_attr_value_name' => $value['sec_attr_value_name'],
            );
        }

        return $this->addAll($data);

    }

    /**
     * 获取sku详情
     * Created By caicm
     * @param int $goods_id 商品id
     * @param int $main_attr_value_id 主属性值id
     * @param int $sec_attr_value_id 次属性值id
     * @return mixed
     */
    public function find_sku_info($goods_id=0, $main_attr_value_id=0, $sec_attr_value_id=0){

        $where = array(
            'goods_id' => $goods_id,
            'main_attr_value_id' => $main_attr_value_id,
            'sec_attr_value_id' => $sec_attr_value_id,
            'is_del' => 0
        );

        if($main_attr_value_id==0) unset($where['main_attr_value_id']);
        if($sec_attr_value_id==0) unset($where['sec_attr_value_id']);

        return $this->where($where)->find();

    }

    /**
     * 获取要删除sku数据
     * Created By caicm
     * @param int $goods_id
     * @param string $sku_id
     * @return mixed
     */
    public function find_del_sku($goods_id=0, $sku_id=''){

        $where = array(
            'sku_id' => array('not in',$sku_id),
            'goods_id' => $goods_id
        );
        if($sku_id=='') unset($where['sku_id']);

        return $this->where($where)->field('sku_id')->select();

    }

    /**
     * 删除sku
     * Created By caicm
     * @param int $goods_id
     * @param string $sku_id
     * @return bool
     */
    public function del_sku($goods_id=0, $sku_id=''){

        $where = array(
            'sku_id' => array('in', $sku_id),
            'goods_id' => $goods_id
        );

        $data = array(
            'is_del' => 1,
            'update_time' => gmtime()
        );

        return $this->where($where)->data($data)->save();

    }

    /**
     * 编辑sku
     * Created By caicm
     * @param array $sku_data
     * @return bool
     */
    public function edit_sku($sku_data=array()){

        foreach ($sku_data as $key=>$value){

            $where = array(
                'sku_id' => $value['sku_id']
            );

            $data = array(
                'goods_money' => $value['goods_money'],
                'main_attr_id' => $value['main_attr_id'],
                'main_attr_name' => $value['main_attr_name'],
                'main_attr_value_id' => $value['main_attr_value_id'],
                'main_attr_value_name' => $value['main_attr_value_name'],
                'sec_attr_id' => $value['sec_attr_id'],
                'sec_attr_name' => $value['sec_attr_name'],
                'sec_attr_value_id' => $value['sec_attr_value_id'],
                'sec_attr_value_name' => $value['sec_attr_value_name'],
                'is_del' => 0,
                'update_time' => gmtime()

            );

            $edit_state = $this->where($where)->data($data)->save();

            if(!$edit_state){
                return false;
            }
        }

        return true;

    }

    /**
     * 获取商品主属性列表
     * Created By caicm
     * @param int $goods_id
     * @return mixed
     */
    public function find_main_attr_list($goods_id=0){

        $where = array(
            'goods_id' => $goods_id,
            'is_del' => 0,

        );

        return $this->where($where)->field('main_attr_value_id,main_attr_value_name,goods_money')->group('main_attr_value_id')->select();

    }

    /**
     * 获取商品次属性列表
     * Created By caicm
     * @param int $goods_id
     * @param int $main_attr_value_id
     * @return mixed
     */
    public function find_sec_attr_list($goods_id=0, $main_attr_value_id=0){

        $where = array(
            'goods_id' => $goods_id,
            'main_attr_value_id' => $main_attr_value_id,
            'is_del' => 0,

        );
        if($main_attr_value_id==0) unset($where['main_attr_value_id']);

        return $this->where($where)->field('sec_attr_value_id,sec_attr_value_name,goods_money')->group('sec_attr_value_id')->select();

    }

    /**
     * 获取商品sku列表
     * Created By caicm
     * @param int $goods_id
     * @return mixed
     */
    public function find_sku_list($goods_id=0){
        $where = array(
            'goods_id' => $goods_id,
            'is_del' => 0,

        );

        return $this->where($where)->field('main_attr_value_id,sec_attr_value_id,goods_money')->select();

    }


}